package com.wizz.trade.trading.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wizz.trade.common.PageVo;
import com.wizz.trade.common.constant.TransactionOcConstant;
import com.wizz.trade.information.mapper.*;
import com.wizz.trade.trading.dto.TransactionDto;
import com.wizz.trade.trading.dto.page.TransactionPageDto;
import com.wizz.trade.trading.entity.Transaction;
import com.wizz.trade.trading.mapper.TransactionMapper;
import com.wizz.trade.trading.service.TransactionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wizz.trade.trading.vo.TransactionVo;
import com.wizz.trade.util.PageUtil;
import com.wizz.trade.util.SeqIdGenerator;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 期货交易表 服务实现类
 * </p>
 *
 * @author fyftless
 * @since 2023-08-28 02:16:03
 */
@Service
public class TransactionServiceImpl extends ServiceImpl<TransactionMapper, Transaction> implements TransactionService {

    @Resource
    private TransactionMapper transactionMapper;

    @Resource
    private SeqIdGenerator seqIdGenerator;

    @Resource
    private VarietyMapper varietyMapper;

    @Resource
    private GradeMapper gradeMapper;

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private CompanydeptMapper companydeptMapper;

    @Resource
    private CurrencyMapper currencyMapper;

    @Override
    public Integer addTransaction(TransactionDto transactionDto) {
        Transaction transaction = new Transaction();
        BeanUtils.copyProperties(transactionDto, transaction);

        // 生成交易单号
        StringBuilder sb1 = new StringBuilder();
        if(transactionDto.getOc().equals(TransactionOcConstant.OPEN)){
            sb1.append("FO");
        }else {
            sb1.append("FC");
        }
        sb1.append(String.format("%02d", transaction.getLedger()));
        sb1.append(String.format("%02d", transaction.getVariety()));
        sb1.append(transaction.getDate().format(DateTimeFormatter.ofPattern("yyMMdd")));
        String prefix1 = sb1.toString();
        String seq1 = seqIdGenerator.generateAutoIncreaseSeqId(prefix1, 3);
        transaction.setTransactionno(prefix1 + seq1);
        sb1.setLength(0);

        // 生成头寸号
        sb1.append(String.format("%02d", transaction.getLedger()));
        sb1.append(String.format("%02d", transaction.getOurdept()));
        sb1.append(String.format("%02d", transaction.getFutures()));
        sb1.append(transaction.getDate().format(DateTimeFormatter.ofPattern("yyMMdd")));
        String prefix2 = sb1.toString();
        transaction.setPositionno(prefix2 + seqIdGenerator.generateAutoIncreaseSeqId(prefix2, 3));

        // 重量单位
        transaction.setUnit(varietyMapper.selectById(transaction.getVariety()).getUnit());

        transactionMapper.insert(transaction);
        return transaction.getId();
    }

    @Override
    public void deleteTransaction(Transaction transaction) {
        transactionMapper.updateById(transaction);
    }

    @Override
    public void deleteTransaction(Integer id) {
        transactionMapper.deleteById(id);
    }

    @Override
    public PageVo<TransactionVo> getPage(TransactionPageDto transactionPageDto) {
        LambdaQueryWrapper<Transaction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(transactionPageDto.getDate()), Transaction::getDate, transactionPageDto.getDate())
                .eq(Objects.nonNull(transactionPageDto.getHedgetype()), Transaction::getHedgetype, transactionPageDto.getHedgetype())
                .eq(Objects.nonNull(transactionPageDto.getLedger()), Transaction::getLedger, transactionPageDto.getLedger())
                .eq(Objects.nonNull(transactionPageDto.getOurdept()), Transaction::getOurdept, transactionPageDto.getOurdept())
                .eq(Objects.nonNull(transactionPageDto.getFutures()), Transaction::getFutures, transactionPageDto.getFutures())
                .eq(Objects.nonNull(transactionPageDto.getVariety()), Transaction::getVariety, transactionPageDto.getVariety())
                .eq(Objects.nonNull(transactionPageDto.getGrade()), Transaction::getGrade, transactionPageDto.getGrade())
                .likeRight(StringUtils.isNotBlank(transactionPageDto.getContract()), Transaction::getContract, transactionPageDto.getContract())
                .likeRight(StringUtils.isNotBlank(transactionPageDto.getPositionno()), Transaction::getPositionno, transactionPageDto.getPositionno());
        Page<Transaction> page = transactionMapper.selectPage(PageUtil.initPage(transactionPageDto), queryWrapper);
        List<Transaction> records = page.getRecords();
        List<TransactionVo> result = new ArrayList<>();
        for (Transaction record : records) {
            TransactionVo transactionVo = new TransactionVo();
            BeanUtils.copyProperties(record, transactionVo);
            transactionVo.setVariety(varietyMapper.selectById(record.getVariety()).getName());
            transactionVo.setGrade(gradeMapper.selectById(record.getGrade()).getName());
            transactionVo.setLedger(companyMapper.selectById(record.getLedger()).getShortname());
            transactionVo.setOurdept(companydeptMapper.selectById(record.getOurdept()).getName());
            transactionVo.setFutures(companyMapper.selectById(record.getFutures()).getShortname());
            transactionVo.setCurrency(currencyMapper.selectById(record.getCurrency()).getName());
            result.add(transactionVo);
        }
        return new PageVo<>(page.getPages(), page.getTotal(), result);
    }
}
